﻿#region - Class details -
/*  
 *  Developer   : Wicus Botha
 *  Description : 
 *  History     : WB - 2009-11-25 - Created
 */
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace IQ.Base.Services
{
    public class Settings
    {
        #region # Constructor #
        public Settings()
        {
        }
        #endregion

        //#region - Private variables -
        //#endregion

        #region - Private methods -
        // NOTE: ConfigurationManager does not save to "exe.vshost.config" file
#if DEBUG
        private static string getAppName()
        {
            return AppDomain.CurrentDomain.FriendlyName.Replace(".vshost", "") + ".Config";
        }
        private static object readSetting(string settingName)
        {
            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
            fileMap.ExeConfigFilename = getAppName();
            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            AppSettingsSection ap = config.AppSettings;
            if (ap.Settings[settingName] != null)
                return ap.Settings[settingName].Value;
            else
                return null;
        }
#else
#endif
        #endregion

        //#region - Public properties / variables -
        //#endregion

        #region - Public methods -
        /// <summary>
        /// Read a string setting from app.config file, with a default value
        /// </summary>
        /// <param name="settingName"> Setting to read</param>
        /// <param name="emptyValue"> Return value if settings was emtpty</param>
        /// <returns>Setting value, if empty returns emptyValue</returns>
        public static string ReadSettingString(string settingName, string emptyValue)
        {
#if DEBUG
            object tmp = readSetting(settingName);
#else
            object tmp = ConfigurationSettings.AppSettings[settingName];
#endif
            if (tmp != null)
                return tmp.ToString();
            else
                return emptyValue;
        }
        public static string ReadSettingEncrypt(string settingName, string emptyValue)
        {
            string tmp = ReadSettingString(settingName, emptyValue);
            return IQ.BaseWindows.General.GetStringED(tmp, 'D');
        }

        public static int ReadSettingInt(string settingName, int emptyValue)
        {
#if DEBUG
            object tmp = readSetting(settingName);
#else
            object tmp = ConfigurationSettings.AppSettings[settingName];
#endif
            if (tmp != null)
            {
                try
                {
                    return Convert.ToInt32(tmp);
                }
                catch
                {
                    return emptyValue;
                }
            }
            else
                return emptyValue;
        }
        //
        public static void SaveSetting(string settingName, string value)
        {
#if DEBUG
            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
            fileMap.ExeConfigFilename = getAppName();
            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
#else
            // Open App.Config of executable.
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( (ConfigurationUserLevel.None);
#endif
            if (config != null)
            {
                // Check if setting exists
                if (config.AppSettings.Settings[settingName] == null)
                {
                    // Add an Application Setting.
                    config.AppSettings.Settings.Remove(settingName);
                    config.AppSettings.Settings.Add(settingName, value);
                }
                else
                {
                    config.AppSettings.Settings[settingName].Value = value;
                }
                // Save the configuration file.
                config.Save(ConfigurationSaveMode.Modified);
                // Force a reload of a changed section.
                ConfigurationManager.RefreshSection("appSettings");
            }
        }

        public static void SaveSettingEncrypt(string settingName, string value)
        {
            string tmp = IQ.BaseWindows.General.GetStringED(value, 'E');
            SaveSetting(settingName, tmp);
        }

        #endregion
    }
}
